/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.pixlib.sfe.view.editor 
{
	import net.pixlib.commands.Delegate;
	import net.pixlib.plugin.Plugin;
	import net.pixlib.sfe.event.EventList;
	import net.pixlib.sfe.util.getTranslation;
	import net.pixlib.sfe.view.ViewList;
	import net.pixlib.sfe.view.abstract.AsWingView;
	import net.pixlib.tms.LocalManager;
	import net.pixlib.tms.LocalManagerEvent;

	import org.aswing.JMenu;
	import org.aswing.JMenuBar;
	import org.aswing.JMenuItem;
	import org.aswing.JSeparator;

	import flash.events.Event;
	import flash.filters.DropShadowFilter;

	/**
	 * Application menubar view.
	 * 
	 * @author Romain Ecarnot
	 */
	public class MenubarView extends AsWingView 
	{
		//--------------------------------------------------------------------
		// Private properties
		//--------------------------------------------------------------------
		
		/** @private */
		private var _fileMenu : JMenu; 

		/** @private */
		private var _newItem : JMenuItem; 

		/** @private */
		private var _loadItem : JMenuItem;

		/** @private */
		private var _saveItem : JMenuItem; 

		/** @private */
		private var _saveAsItem : JMenuItem; 

		/** @private */
		private var _exitItem : JMenuItem; 

		/** @private */
		private var _helpMenu : JMenu; 

		/** @private */
		private var _aboutItem : JMenuItem; 

		/** @private */
		private var _prefItem : JMenuItem; 
		
		/** @private */
		private var _updateItem : JMenuItem; 

		
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Creates view.
		 */
		public function MenubarView(owner : Plugin)
		{
			super(owner, ViewList.MENUBAR_VIEW, new JMenuBar());
		}

		
		//--------------------------------------------------------------------
		// Protected methods
		//--------------------------------------------------------------------
		
		/**
		 * Inits User Interface.
		 */
		override protected function onInitView() : void
		{
			LocalManager.getInstance().addEventListener(LocalManagerEvent.onLocalChangeEVENT, _onLocalChangeHandler);
			
			var menubar : JMenuBar = getComponent() as JMenuBar;
			menubar.setOpaque(true);
			menubar.setPreferredHeight(25);
			
			getComponent().filters = [new DropShadowFilter(1, 90, 0x000000, 0.35, 5, 5)];
			
			//File menu
			_fileMenu = menubar.addMenu(new JMenu("File"));
			
			_newItem = _fileMenu.addMenuItem("New library project");
			_newItem.addActionListener(Delegate.create(_onExecuteCommand, EventList.NEW_PROJECT));
			
			_loadItem = _fileMenu.addMenuItem("Open library project");
			_loadItem.addActionListener(Delegate.create(_onExecuteCommand, EventList.LOAD_PROJECT));
			
			_fileMenu.append(new JSeparator());
			
			_saveItem = _fileMenu.addMenuItem("Save library project");
			_saveItem.addActionListener(Delegate.create(_onExecuteCommand, EventList.SAVE_PROJECT));
			
			_saveAsItem = _fileMenu.addMenuItem("Save library project as...");
			_saveAsItem.addActionListener(Delegate.create(_onExecuteCommand, EventList.SAVE_AS_PROJECT));
			
			_fileMenu.append(new JSeparator(JSeparator.HORIZONTAL));
			
			_exitItem = _fileMenu.addMenuItem("Exit");
			_exitItem.addActionListener(Delegate.create(_onExecuteCommand, EventList.EXIT));
			
			//Options menu
			_helpMenu = menubar.addMenu(new JMenu("?"));
			
			_prefItem = _helpMenu.addMenuItem("Preferences");
			_prefItem.addActionListener(Delegate.create(_onExecuteCommand, EventList.OPEN_PREFERENCES_WINDOW));
			
			_updateItem = _helpMenu.addMenuItem("Check for updates");
			_updateItem.addActionListener(Delegate.create(_onExecuteCommand, EventList.CHECK_UPDATE));
			
			_helpMenu.append(new JSeparator(JSeparator.HORIZONTAL));
			
			_aboutItem = _helpMenu.addMenuItem("About");
			_aboutItem.addActionListener(Delegate.create(_onExecuteCommand, EventList.OPEN_ABOUT_WINDOW));
			
			_onLocalChangeHandler();
			
			super.onInitView();	
		}

		
		//--------------------------------------------------------------------
		// Private methods
		//--------------------------------------------------------------------
		
		/** @private */
		private function _onExecuteCommand(event : Event, eventType : String) : void
		{
			firePrivateEvent(new Event(eventType));	
		}

		/** @private */
		private function _onLocalChangeHandler(event : Event = null) : void
		{
			_fileMenu.setText(getTranslation("fileMenuLabel"));
			_fileMenu.repaintAndRevalidate();
			
			_newItem.setText(getTranslation("newFileItemLabel"));			_loadItem.setText(getTranslation("loadFileItemLabel"));			_saveItem.setText(getTranslation("saveFileItemLabel"));			_saveAsItem.setText(getTranslation("saveAsFileItemLabel"));
			_exitItem.setText(getTranslation("exitFileItemLabel"));
			
			_helpMenu.setText(getTranslation("helpMenuLabel"));
			_prefItem.setText(getTranslation("prefHelpItemLabel"));			_updateItem.setText(getTranslation("prefUpdateItemLabel"));
			_aboutItem.setText(getTranslation("aboutHelpItemLabel"));
		}
	}
}
